import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/person/userUnion'

const api = {

    /**
     * 新增员工联合信息
     * @param userUnion
     * @returns {Promise<*|undefined>}
     */
    createUserUnion: (userUnion) =>
      axiosClient().post(`${requestUrl}/addInfo`, userUnion),

    /**
     * 根据ID删除员工联合信息
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteUserUnionById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新员工联合信息
     * @param id
     * @param userUnion
     * @returns {Promise<*|undefined>}
     */
    updateUserUnionFull: (id, userUnion) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, userUnion),

    /**
     * 根据ID部分更新员工联合信息
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateUserUnionPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询员工联合信息
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getUserUnionById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询员工联合信息
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserUnionPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询员工联合信息
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getUserUnionList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.userIdentityId) {
            params.append('userIdentityId', businessParam.userIdentityId)
        }
        if (businessParam.userBaseId) {
            params.append('userBaseId', businessParam.userBaseId)
        }
        if (businessParam.userAccountId) {
            params.append('userAccountId', businessParam.userAccountId)
        }
        if (businessParam.orgLevelId) {
            params.append('orgLevelId', businessParam.orgLevelId)
        }
        if (businessParam.orgId) {
            params.append('orgId', businessParam.orgId)
        }
        if (businessParam.postDefId) {
            params.append('postDefId', businessParam.postDefId)
        }
        if (businessParam.postId) {
            params.append('postId', businessParam.postId)
        }
        if (businessParam.headshipId) {
            params.append('headshipId', businessParam.headshipId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
